package com.wpml.netty.test;

import java.sql.*;

/**
 * @Description
 * @Date 2021-08-12
 * @Author wangpei
 **/
public class JDBCDemo {

    static String url = "jdbc:mysql://8.141.50.71:3306/msb_uc";
    static String username = "root";
    static String password = "root";

    public static void main(String[] args) throws Exception {

        Class.forName("com.mysql.jdbc.Driver");

        Connection connection = DriverManager.getConnection(url, username, password);

        DatabaseMetaData metaData = connection.getMetaData();
        ResultSet columns = metaData.getColumns("msb_uc", null, "sys_dict", null);
        while (columns.next()) {
            String dbName = columns.getString(1); // 数据库名
            String tb_name = columns.getString(3); // 表名
//            String colName = columns.getString(4); // 字段名
            String colName = columns.getString("COLUMN_NAME"); // 字段名
//            String colType = columns.getString(6); // 字段属性
            String colType = columns.getString("TYPE_NAME"); // 字段属性
//            String colRemark = columns.getString(12); // 字段备注
            String colRemark = columns.getString("REMARKS"); // 字段备注
            System.out.println(String.format("%s\t%s\t%s\t%s\t%s", dbName, tb_name, colName, colType, colRemark));
        }
//        ResultSet tables = metaData.getTables("msb_uc", null, null, null);
//        while (tables.next()) {
//            String tbName = tables.getString(3);
//            System.out.println("tbName ================= " + tbName);
//
//            ResultSet columns = metaData.getColumns("msb_uc", null, tbName, null);
//            while (columns.next()) {
//                String dbName = columns.getString(1); // 数据库名
//                String tb_name = columns.getString(3); // 表名
//                String colName = columns.getString(4); // 字段名
//                String colType = columns.getString(6); // 字段属性
//                String colRemark = columns.getString(12); // 字段备注
//                /**
//                 * msb_uc	allocate_headmaster_relation	id	BIGINT	主键
//                 * msb_uc	allocate_headmaster_relation	allocate_user_id	VARCHAR	分配用户id
//                 * msb_uc	allocate_headmaster_relation	headmaster_id	VARCHAR	班主任id
//                 */
//                System.out.println(String.format("%s\t%s\t%s\t%s\t%s", dbName, tb_name, colName, colType, colRemark));
//            }
//        }

        // 查询数据
        /*Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from sys_dict");

        while (resultSet.next()) {
            System.out.println(resultSet.getString(2));
            System.out.println(resultSet.getString("type"));
        }*/
    }
}
